/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package optimizacion.problemas;

import Distribuciones.Uniforme;
import ec.util.MersenneTwisterFast;
import optimizacion.Function;

/**
 *
 * @author USUARIO
 */
public class EjemploSeno {
    public static void main(String[] args) {
        MersenneTwisterFast prng = new MersenneTwisterFast(56879542);
        Uniforme u1 = new Uniforme(0,2*Math.PI);
        
        double fopt = 0D;
        double xopt = 0D;
        double deltaX= (2 * Math.PI)/10000D;
        int n = 100;
        for (int i = 0; i < n; i++) {
            double xi = u1.generar(prng.nextDouble());
            double yi = Function.fx(xi);
            double xi_plus = xi + deltaX;
            double xi_minus = xi - deltaX;
            boolean direccion= true;
            int delta = 1;
            if ((Function.fx(xi_plus) > yi) && (Function.fx(xi_plus) > Function.fx(xi_minus))) {
                direccion = true;
                delta= 1;
            } else if ((Function.fx(xi_minus) > yi) && (Function.fx(xi_plus) < Function.fx(xi_minus))){
                direccion = true;
                delta= -1;
            } else {
                direccion = false;
                delta= 0;
            }
            
            double xi_opt_i = xi;
            double fi_opt_i = yi;
            
            if (direccion) {
                double deltaO = delta*deltaX;
                while ((Function.fx(xi_opt_i+ deltaO) > fi_opt_i) && (xi_opt_i+ deltaO > 0) && (xi_opt_i+ deltaO < 2*Math.PI)) {
                    xi_opt_i += deltaO;
                    fi_opt_i =Function.fx(xi_opt_i);
                }
            }
            if (fi_opt_i > fopt) {
                fopt = fi_opt_i;
                xopt = xi_opt_i;
            }
            
        }
        System.out.println("x maximo: " + xopt);
        System.out.println("f(x) maximo: "+ fopt);
    }
}
